<?php
/*
 * Created on May 7, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */


class embood {
   public $myLink;
   public $myDescription;
   public $myTitle;
   public $myEmbedcode;
   
   
   public function extractURL($text) {
   	preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$text,$matches);
   	$url = $matches[0];
   	return $url;
   }
      public function __construct($description){
   		$this->myDescription = $description;
   		$this->myLink=$this->extractURL($this->myDescription);
   		$fetcheddata = $this->createEmboodle($this->myLink); //fetches embedcode, perhaps t
	   	//parse out url if there is one    
	   	$this->myTitle=$fetcheddata['title'];
	   	$this->myEmbedcode=$fetcheddata['embedcode'];  
   }
   
   public function createEmboodle($url) {
	   	$url_parts = parse_url($url);
		$domain=$url_parts['host'];
		//fish out just the url
	
	    if(preg_match('/youtube\.com/',$url)){
	    	$type='youtube';
	    	preg_match(
	        	'/[\\?\\&]v=([^\\?\\& ]+)/',
	        	$url,
	        	$matches);
	   
	   		$id=$matches[1];	 	
			//echo $matches[1];  
			$embedcode = '
			<object width="560" height="340">
			<param name="movie" value="http://www.youtube.com/v/'.$id.'=en&fs=1&">
			</param><param name="allowFullScreen" value="true">
			</param><param name="allowscriptaccess" value="always">
			</param>' .
					'<embed src="http://www.youtube.com/v/'.$id.'&hl=en&fs=1&" 
				type="application/x-shockwave-flash" 
				allowscriptaccess="always" 
				allowfullscreen="true" 
				width="560" 
				height="340">
			</embed>
			</object>';
	    }
	    
	    if($domain=='www.slideshare.net') {
	    	$type='flicker';
	    	$apiobj = new SSUtil();
	 		$slideObj=$apiobj->get_slideInfoByUrl($url);
	 		//if slideojb exists...TODO FIXME
	 		$embedcode = $slideObj['EMBED'];
	    }
	    if($domain=='www.flickr.net'){
	    	
	    	//select * from html where url="http://www.slideshare.net/cheilmann/planning-javascript-and-ajax-for-larger-teams-174408" and xpath='/html/body/div[2]/div[4]/div/p[2]/span[2]/span/script'
	    	//
	    	//http://query.yahooapis.com/v1/public/yql?q=select%20src%2Calt%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.flickr.com%2Fphotos%2Fabsolutely_loverly%2F404251324%2Fin%2Fphotostream%2F'%20and%20xpath%3D%22%2F%2Fdiv%5B%40class%3D'photoImgDiv'%5D%2Fimg%22&format=xml
	    	//select src,alt from html where url='http://www.flickr.com/photos/absolutely_loverly/404251324/in/photostream/' and xpath="//div[@class='photoImgDiv']/img"
	    }
	    $array = array('embedcode' => $embedcode, 'title'=>'testtitle','type'=>$type);
	    return $array;   
	}
}
?>
